package com.institute.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.ExtjsAjaxResponse;
import com.common.util.ExtjsPageable;
import com.common.util.TreeNode;
import com.institute.entity.Institute;
import com.institute.entity.dto.InstituteDTO;
import com.institute.entity.dto.PrincipalDTO;
import com.institute.service.IInstituteService;
import com.staff.entity.Staff;
import com.staff.service.IStaffService;
import com.syslog.aop.SystemServiceLog;

@Controller
@RequestMapping("/institute")
public class InstituteController {
	@Autowired
	private IInstituteService instituteService;
	@Autowired
	private IStaffService staffService;
	
	@SystemServiceLog("查找部门返回树")
	@RequestMapping("/findTree")
	public @ResponseBody TreeNode findTree(){
		/*TreeNode treeNode = new TreeNode<InstituteDTO>();
		List<Institute> list = instituteService.findAll();
		List<InstituteDTO> dtos = new ArrayList<InstituteDTO>();
		for(Institute i:list) {
			Institute father = instituteService.findFather(instituteService.findOne(i.getId()));
			Long fatherId = null;
			if(father!=null)fatherId = father.getId();
			Staff prinsipal = instituteService.getPrinsipal(i);
			String prinsipalName = null;
			if(null!=prinsipal)prinsipalName = prinsipal.getStaffName();
			InstituteDTO dto = new InstituteDTO(i, instituteService.getStaffNum(i), prinsipalName,fatherId );
			dtos.add(dto);
		}
		treeNode = InstituteDTO.getTreeNode(dtos);
		return treeNode;*/
		try {
			return instituteService.findTree();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	@RequestMapping("/findAll")
	public @ResponseBody List<InstituteDTO> findAll(){
		List<Institute> list = instituteService.findAll();
		List<InstituteDTO> dtos = new ArrayList();
		for(Institute i:list) {
			Institute father = instituteService.findFather(instituteService.findOne(i.getId()));
			Long fatherId = null;
			if(father!=null)fatherId = father.getId();
			Staff prinsipal = instituteService.getPrinsipal(i);
			String prinsipalName = null;
			if(null!=prinsipal)prinsipalName = prinsipal.getStaffName();
			InstituteDTO dto = new InstituteDTO(i, instituteService.getStaffNum(i), prinsipalName,fatherId );
			dtos.add(dto);
		}
		return dtos;
		
	}
	@RequestMapping("/save")
	public @ResponseBody ExtjsAjaxResponse save(InstituteDTO dto) {
		try {
			if(instituteService.isNameExist(dto))
				return new ExtjsAjaxResponse(false,"部门名称已存在！");
			if(instituteService.isCodeExist(dto))
				return new ExtjsAjaxResponse(false,"部门编号已存在！");
			instituteService.saveOrUpdate(dto);
			return new ExtjsAjaxResponse(true,"添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"添加失败！");
		}
	}
	@RequestMapping("/findStaffByDept")
	public @ResponseBody Page<PrincipalDTO> findStaffByDept(PrincipalDTO dto,ExtjsPageable pageable){
		if(dto.getDeptId()==null)return null;
		return instituteService.findStaffByDept(dto, pageable.getPageable());
		
	}
	@RequestMapping("/update")
	public @ResponseBody ExtjsAjaxResponse update(InstituteDTO dto) {
		try {
			if(instituteService.isNameExist(dto))
				return new ExtjsAjaxResponse(false,"部门名称已存在！");
			if(instituteService.isCodeExist(dto))
				return new ExtjsAjaxResponse(false,"部门编号已存在！");
			instituteService.saveOrUpdate(dto);
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtjsAjaxResponse delete(Long id) {
		
		try {
			if(instituteService.hasChildren(id))return new ExtjsAjaxResponse(true,"请先移除子部门！");
			if(instituteService.hasPositions(id))return new ExtjsAjaxResponse(true,"请先移除部门下的职务！");
			if(instituteService.hasStaffs(id))return new ExtjsAjaxResponse(true,"请先移除部门下的员工！");
			instituteService.deleteInstitute(id);
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/combine")
	public @ResponseBody ExtjsAjaxResponse combine(Long fatherId,Long deptId) {
		try {
			if(fatherId == deptId)return new ExtjsAjaxResponse(false,"不能合并到自身！");
			else if(instituteService.isSubordinateDept(deptId, fatherId))return new ExtjsAjaxResponse(false,"不能合并到自身的下级部门！");
			else {
				instituteService.combineInstitute(fatherId,deptId );
				return new ExtjsAjaxResponse(true,"操作成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/move")
	public @ResponseBody ExtjsAjaxResponse move(Long fatherId,Long childId) {
		try {
			if(fatherId == childId)return new ExtjsAjaxResponse(false,"不能移动到自身！");
			else if(instituteService.isSubordinateDept(childId, fatherId))return new ExtjsAjaxResponse(false,"不能移动到自身的下级部门！");
			else {
				instituteService.moveInstitute(fatherId,childId );
				return new ExtjsAjaxResponse(true,"操作成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/selectPrincipal")
	public @ResponseBody ExtjsAjaxResponse selectPrincipal(Long deptId,Long staffId) {
		try {
			instituteService.selectPrincipal(deptId, staffId);
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
		
	}
	@RequestMapping("/findCombineDept")
	public @ResponseBody List<Institute> findCombineDept(Long deptId){
		if(deptId == null)return null;
		return instituteService.findCombineDept(deptId);
	}
	@RequestMapping("/findMoveDept")
	public @ResponseBody List<Institute> findMoveDept(Long deptId){
		if(deptId == null)return null;
		return instituteService.findMoveDept(deptId);
	}
}
